package service

import (
	"ksd-social-api/commons/base/service"
	"ksd-social-api/commons/page"
	"ksd-social-api/modules/channel/model"
	"ksd-social-api/modules/channel/model/context"
	"ksd-social-api/modules/channel/model/vo"
	context3 "ksd-social-api/modules/msg/model/context"
	"ksd-social-api/utils"
	"strconv"
	"time"
)

type AdminChannelService struct {
	service.BaseService
}

/**
 * @author feige
 * @date 2023-10-13
 * @version 1.0
 * @desc 查询资源列表信息，栏目和搜索使用
 */
func (service *AdminChannelService) FindChannelListAndPage(ctx context.ChannelSearchContext) *page.Page {
	p := adminChannelMapper.FindChannelListAndPage(ctx)
	channels := p.Records.([]model.Channel)
	channelVos := []vo.ChannelVo{}
	for _, channel := range channels {
		// 3: 查询当前登录的用户信息和资源id去查询用户资源收藏表
		channelVo := vo.ChannelVo{}
		utils.CopyProperties(&channelVo, channel)
		// 2: 如果没有错误就必须把model数据导入vo数据中
		channelVos = append(channelVos, channelVo)
	}

	p.Records = channelVos
	return p
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  修改状态
 */
func (service *AdminChannelService) UpdateFieldById(ctx context.ChannelUpdateContext) bool {
	return adminChannelMapper.UpdateFieldById(ctx.Id.Uint64(), ctx.Field, ctx.Value, ctx.SystemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  修改状态
 */
func (service *AdminChannelService) UpdateFieldByCodeId(ctx context.ChannelUpdateContext) bool {
	return adminChannelMapper.UpdateFieldByCodeId(ctx.Id.Uint64(), ctx.Field, ctx.Value, ctx.SystemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  修改状态
 */
func (service *AdminChannelService) SaveUpdateChannel(ctx context.ChannelContext) bool {
	// 保存入库
	var channel model.Channel
	utils.CopyProperties(&channel, ctx)
	channel.Id = ctx.Id.Uint64()
	data := adminChannelMapper.SaveUpdateChannel(channel)
	return data != nil
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存和修改兑换码
 */
func (service *AdminChannelService) SaveUpdateChannelCode(ctx context.ChannelCodeContext) *vo.ChannelCodeVO {
	// 保存入库
	var channelCode model.ChannelCode
	var channelVoCode vo.ChannelCodeVO
	utils.CopyProperties(&channelCode, ctx)
	parseUint, _ := strconv.ParseUint(ctx.Id, 10, 64)
	channelCode.Id = parseUint
	channelCode.Opid = ctx.Opid.Uint64()
	data := adminChannelMapper.SaveUpdateChannelCode(channelCode)
	utils.CopyProperties(&channelVoCode, data)
	return &channelVoCode
}

/**
 * @author feige
 * @date 2023-10-13
 * @version 1.0
 * @desc 查看兑换码
 */
func (service *AdminChannelService) FindChannelCodePage(ctx context.CodeSearchContext) *page.Page {
	p := adminChannelMapper.FindChannelCodePage(ctx)
	channelCodes := p.Records.([]model.ChannelCode)
	channelCodeVos := []vo.ChannelCodeVO{}
	for _, channelCode := range channelCodes {
		// 3: 查询当前登录的用户信息和资源id去查询用户资源收藏表
		channelCodeVo := vo.ChannelCodeVO{}
		utils.CopyProperties(&channelCodeVo, channelCode)
		// 2: 如果没有错误就必须把model数据导入vo数据中
		channelCodeVos = append(channelCodeVos, channelCodeVo)
	}
	p.Records = channelCodeVos
	return p
}

/**
 * 批量授权用户
 * @author feige
 * @date 2023-12-06
 * @version 1.0
 * @desc
 */
func (service *AdminChannelService) SaveUserChannelOrder(ctx context.BatchUsersContext) bool {
	flag := true
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	userVo := userMapper.GetUserInfoByUuid(ctx.Uuid, ctx.SystemId)
	userId := userVo.Id
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	isBuyFlag := userBuyChannelMapper.CountUserBuyChannelNo(userId, ctx.Opid.Uint64(), ctx.SystemId)
	if isBuyFlag == 0 {
		// 根据小册id查询小册信息
		channel, _ := channelMapper.GetById(ctx.Opid.Uint64(), ctx.SystemId)
		userVo := userMapper.GetUserByID(userId, ctx.SystemId)

		userBuyChannel := model.UserBuyChannel{}
		OutTradeNo := service.GetSnowWorkerIdString(8)
		// 处理返回结果
		userBuyChannel.ChannelId = ctx.Opid.Uint64()
		userBuyChannel.Title = channel.Title
		userBuyChannel.Cover = ""

		userBuyChannel.UserId = userId
		userBuyChannel.Username = userVo.Username
		userBuyChannel.Avatar = userVo.Avatar
		userBuyChannel.Uuid = userVo.Uuid
		userBuyChannel.Phone = userVo.Telephone
		userBuyChannel.Address = userVo.Address
		userBuyChannel.Nickname = userVo.Nickname

		userBuyChannel.Code = ""
		userBuyChannel.PayMethod = 4
		userBuyChannel.PayMethodName = "人工服务"
		userBuyChannel.Tradeno = OutTradeNo
		userBuyChannel.Orderno = OutTradeNo
		userBuyChannel.OrderJson = ""
		userBuyChannel.SystemId = ctx.SystemId
		userBuyChannel.SystemName = ctx.SystemName
		userBuyChannel.Price = channel.Price
		// 保存订单的接口
		userBuyChannelMapper.SaveUserBuyChannel(userBuyChannel)

		// 保存消息
		messageMeContext := context3.MessageMeContext{}
		messageMeContext.UserId = userId
		messageMeContext.Title = channel.Title
		messageMeContext.Uuid = userVo.Uuid
		messageMeContext.SystemId = ctx.SystemId
		messageMeContext.SystemName = "系统512"
		messageMeContext.Username = userVo.Nickname
		messageMeContext.Url = "https://www.study512.com" + channel.Url
		messageMeService.SaveMessageMeBuyResumeTemplate(&messageMeContext)
	} else {
		flag = false
	}
	return flag
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  根据id删除
 */
func (service *AdminChannelService) DelById(id uint64, systemId uint64) bool {
	return adminChannelMapper.DelById(id, systemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  根据用户id查询用户明细
 */
func (service *AdminChannelService) DelCodeById(id uint64, systemId uint64) bool {
	return adminChannelMapper.DelCodeById(id, systemId)
}

/*
复制
*
*/
func (service *AdminChannelService) CopyData(id uint64, systemId uint64) bool {
	// 保存入库
	data := adminChannelMapper.GetById(id, systemId)
	data.Id = 0
	data.Status = 0
	data.CreateTime = time.Now()
	data.UpdateTime = time.Now()
	adminChannelMapper.SaveUpdateChannel(*data)
	return true
}
